package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckgroupMapper;
import com.itheima.mapper.RoleMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Role;
import com.itheima.service.CheckgroupService;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Override
    public void addGroup(Role role, Integer[] menu) {
        Integer groupId = role.getId();
        if (groupId!=null){
            baseMapper.deleteCheckitemsByGroupId(groupId);
        }
        // saveOrUpdate 此方法主键值为null或者为0为插入,否则为更新 且会自动获取主键值
        saveOrUpdate(role);
        groupId = role.getId();
        if (menu!=null){
            for (Integer roleid : menu) {
                baseMapper.addCheckgroupWithCHeckitem(groupId,roleid);
            }
        }
    }

    @Override
    public Map findPage(QueryPageBean queryPageBean) {
        // 这里要写泛型的
        LambdaQueryWrapper<Role> lambda = new QueryWrapper<Role>().lambda();
        String queryString = queryPageBean.getQueryString();
        queryString=queryString==null?"":queryString;
        lambda.like(Role::getName,queryString);
        // 对于这个业务来说,上下这两行代码必须是这样的顺序 逻辑上才不会出错
        lambda.eq(Role::getIs_delete, 0);

        // 此方法用于分页查询 总记录数与结果集都已封装至结果
        Page<Role> page = page(new Page<Role>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), lambda);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("list",page.getRecords());
        return map;
    }




    public List<Integer> findCheckitemIds(Integer roleId) {
        List<Integer> roleIds = baseMapper.findCheckitemIds(roleId);
        return roleIds;
    }


}


